package game.windows;


import game.shared.Constants;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Buttons;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;

public class Button extends WindowControl {
	protected TextureRegion  tButtonNomal;
	protected TextureRegion  tButtonPressed;
	protected boolean pressed;
	private boolean oldLeftClick, newLeftClick;
	
	public Button(TextureRegion  tButtonNomal, TextureRegion  tButtonPressed)
	{
		this.tButtonNomal = tButtonNomal;
		this.tButtonPressed = tButtonPressed;
		pressed = false;
		oldLeftClick = false;
		this.width = tButtonNomal.getRegionWidth();
		this.height =  tButtonNomal.getRegionHeight();
	}
	
	@Override
	public void update() {
		if(isOver())
		{
			newLeftClick = Gdx.input.isButtonPressed(Buttons.LEFT);
			if(newLeftClick == true && oldLeftClick == false)
				pressed = !pressed;
			oldLeftClick = newLeftClick;
		}
	}
	
	public boolean isOver()
	{
		int x = Gdx.input.getX();
		int y = Constants.SCREEN_HEIGHT - Gdx.input.getY();
		
		if((this.x<x) && (x<this.x+this.width))
			if((this.y<y) && (y<this.y+this.height))
				return true;
		return false;
	}
	
	@Override
	public void render(SpriteBatch sb) {
		if(pressed)
		{
			if(tButtonPressed != null)
				sb.draw(tButtonPressed, x, y);
		}
		else
		{
			if(tButtonNomal != null)
				sb.draw(tButtonNomal, x, y);
		}
	}
}
